package com.sys.controller;

import com.sys.bean.RespBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/admin/uploadController")
public class UploadController {
    @Value("${sys.picdir}")
    private String picdir;
    //只考虑单图片上传--图片上传是post请求
    @PostMapping
    public RespBean upload(MultipartFile pic) throws IOException {
        //获取图片的名字
        String fileName = pic.getOriginalFilename();
        //将图片的名字拼接成唯一的文件名
        fileName = UUID.randomUUID().toString().replace("-","") + "_" + fileName;
        //保存图片到服务器
        pic.transferTo(new File(picdir + fileName));
        return RespBean.ok("上传成功", fileName);
    }
}
